
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rt.schedulebase.dto.DictRoleDto;
import com.rt.schedulebase.entity.DictRole;
import com.rt.schedulebase.entity.DictRoleFunction;
import com.rt.schedulebase.entity.DictUserRole;
import com.rt.schedulebase.mapper.DictRoleMapper;
import com.rt.schedulenew.api.IDictSerialService;
import com.rt.schedulenew.api.IDictRoleFunctionService;
import com.rt.schedulenew.api.IDictRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulenew.api.IDictUserRoleService;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictRoleServiceImpl extends ServiceImpl<DictRoleMapper, DictRole> implements IDictRoleService {
    @Autowired
    private IDictSerialService dictSerialService;
    @Autowired
    private IDictUserRoleService dictUserRoleService;
    @Autowired
    private IDictRoleFunctionService dictRoleFunctionService;

    @Override
    public boolean insertSelective(DictRole dictRole) {
        return retBool(baseMapper.insert(dictRole));
    }

    @Override
    public boolean deleteByPrimaryKey(String key) {
        return retBool(baseMapper.deleteById(key));
    }

    @Override
    public boolean updateByPrimaryKeySelective(DictRole dictRole) {
        return retBool(baseMapper.updateById(dictRole));
    }

    @Override
    public DictRole selectByPrimaryKey(String key) {
        return baseMapper.selectById(key);
    }

    @Override
    public List<DictRole> getList(DictRoleDto dto) {
        Map<String, Object> map = new HashMap<>();
        map.put("ROLE_ID", dto.getRoleId());
        map.put("REPORT_LEVEL", dto.getReportLevel());
        QueryWrapper<DictRole> qw = new QueryWrapper();
        qw.allEq(map, false);
        if (StringUtils.isNotBlank(dto.getRoleName())) {
            qw.like("ROLE_NAME", dto.getRoleName());
        }
        qw.orderByAsc("ROLE_ID");
        List<DictRole> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<DictRole> getListAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean delete(DictRole dictRole) {
        DictUserRole dictUserRole = new DictUserRole();
        dictUserRole.setRoleId(dictRole.getRoleId());
        dictUserRoleService.delete(dictUserRole);
        DictRoleFunction dictRoleFunction = new DictRoleFunction();
        dictRoleFunction.setRoleId(dictRole.getRoleId());
        dictRoleFunctionService.deleteByCondition(dictRoleFunction);
        UpdateWrapper<DictRole> uw = new UpdateWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("ROLE_ID", dictRole.getRoleId());
        uw.allEq(map);
        return retBool(baseMapper.delete(uw));
    }

    @Override
    public boolean update(DictRole dictRole) {
        UpdateWrapper<DictRole> uw = new UpdateWrapper();
        Map<String, Object> map = new HashMap<>();
        map.put("ROLE_ID", dictRole.getRoleId());
        uw.allEq(map, false);
        return retBool(baseMapper.update(dictRole, uw));
    }

    @Override
    public DictRole getInfo(DictRoleDto dto) {
        QueryWrapper<DictRole> qw = new QueryWrapper();
        Map<String, Object> map = new HashMap<>();
        map.put("ROLE_ID", dto.getRoleId());
        qw.allEq(map, false);
        return baseMapper.selectOne(qw);
    }

    @Override
    public JsonResult insertRole(DictRole dictRole) {
        QueryWrapper<DictRole> qw = new QueryWrapper();
        qw.eq("ROLE_NAME", dictRole.getRoleName());
        int count = baseMapper.selectCount(qw);
        if (count > 0) {
            return JsonResultUtil.failure("添加失败:角色名已存在");
        }
        String id = dictSerialService.getSerialId("ROLE_ID");
        dictRole.setRoleId(id);
        boolean bool = insertSelective(dictRole);
        if (bool) {
            return JsonResultUtil.success();
        }
        return JsonResultUtil.failure();
    }
}
